<?php

namespace app\common\model;

use basic\ModelBasic;

class Goods extends ModelBasic
{
	use \traits\ModelTrait;

    /** 商品类型 */
    public static $goods_type = [
        1 => '出行游',
        2 => '酒店',
        3 => '餐饮聚会',
        6 => '实物商品',
    ];

	 /**
     * 列表
     * @Author   FW
     * @DateTime 2020-09-24
     * @Email    123763712@qq.com
     * @param    [type]
     * @return   [type]
     */
    public static function getList($where){
        $count = self::setWhere($where)->count();
        $data  = [];
        if ( $count == 0 ) {
            return compact('data','count');
        }

        $data=self::setWhere($where,'m')
        ->field("m.*")
        ->page($where['page'] ?? 1,$where['limit'] ?? 10)
        ->select();

        if ( !empty($data) ) {
            $data = $data->toArray();
            $data = static::process_list($data);
        }

        return compact('data','count');
    }

    /**
     * [getOne description]
     * @param    [type]           $id [description]
     * @return   [type]               [description]
     */
    public static function getOne ($id)
    {
        $item = self::alias('m')->where('m.id',$id)
            ->field("m.*")
            ->find();

        if ( empty($item) ) {
            return [];
        }

        $item = $item->toArray();

        $item['detail'] = htmlspecialchars_decode(CommonDetail::where(['rel_type'=>'goods','rel_id'=>$item['id']])->value('detail'));

        return static::process_list([$item])[0];
    }

     /**
     * 处理列表数据
     * @param    array            $list [description]
     * @return   [type]                 [description]
     */
    public static function process_list (array $list)
    {
        foreach ($list as $key => &$value) {
            $value['goods_type_text'] = static::$goods_type[$value['goods_type']] ?? '-';;

            $value['status_text']      = static::$common_status[$value['status']] ?? '-';
        }
        return $list;
    }

	/**
	 * 设置where条件
	 * @Author   FW
	 * @DateTime 2021-01-12
	 * @Email    123763712@qq.com
	 * @param    [type]           $where [description]
	 * @param    string           $alias [description]
	 * @param    [type]           $model [description]
	 */
	public static function setWhere($where,$alias='m',$model=null){
        $model = $model===null ? new self() : $model;

        $model->alias($alias);

        if ( static::$softDelete ) {
        	$model->where($alias.'.is_del',0);
        }

        if ( isset($where['name']) && $where['name'] != '' ) {
            $model->where( sprintf('%s.name' , $alias) . ' LIKE ' . "'%" . $where['name'] ."%'");
        }

        if ( isset($where['goods_type']) && $where['goods_type'] > 0 ) {
            $model->where(sprintf('%s.goods_type = %d' , $alias , $where['goods_type']));
        }

        if ( isset($where['order']) && $where['order'] != '' ) {
            $model->order(sprintf('%s.%s' , $alias, $where['order']));
        } else {
            $model->order(sprintf('%s.id ASC' , $alias));
        }

        return $model;
    }

     /**
     * 主数据保存后回调
     * @Author   FW
     * @DateTime 2021-08-10
     * @Email    123763712@qq.com
     * @param    [type]           $prk_id [description]
     * @param    array            $data   [description]
     * @return   [type]                   [description]
     */
    public static function afterSave ( $prk_id , array $data )
    {
        if ( !isset($data['detail']) ) return true;

        return CommonDetail::saveOne([
            'rel_type'    =>    'goods',
            'rel_id'      =>    $prk_id,
            'detail'      =>    htmlentities($data['detail'])
        ]);
    }

    /**
     * 选择已购套餐预检查以及返回正确数据格式
     * @Author   FW
     * @DateTime 2021-08-11
     * @Email    123763712@qq.com
     * @param    [type]           $id [description]
     */
    public static function ConsumePrecheck ( $svc_id , $bought_id = 0 )
    {
        if ( $bought_id > 0 ) {
            $service = BoughtService::alias('b')
            ->field('0 as price,svc.name as rel_name,b.bought_qty,b.rel_type,b.rel_id')
            ->join('__GOODS__ svc' , 'svc.id = b.rel_id' , 'left')
            ->where(['b.id' => $bought_id, 'b.status' => 1])
            ->find();
        } else {
            $service = Goods::alias('svc')
            ->field("svc.price,svc.name as rel_name,'goods' as rel_type, id as rel_id,999 as bought_qty")
            ->where('id' , $svc_id)
            ->find();
        }

        if ( empty($service) ) throw new \think\exception\ValidateException('校验失败【0901】');

        $valid['item_ukey'] = md5($service->rel_type . '_' . $service->rel_id . '_' . $bought_id);
        $valid['pck_id']    = 0;
        $valid['rel_type']  = $service->rel_type;
        $valid['rel_id']    = $service->rel_id;
        $valid['rel_name']  = $service->rel_name;
        $valid['bought_id'] = $bought_id;
        $valid['price']     = $service->price;
        $valid['qty']       = 1;
        $valid['disc_amt']  = 0;
        $valid['max_nums']  = $service->bought_qty;
        return $valid;
    }
}//Class End
